package com.shanzmoo.stm.admin.web;

import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.db.stm.entity.query.StmSysRoleQuery;
import com.shanzmoo.db.stm.entity.vo.StmSysRoleVO;
import com.shanzmoo.db.stm.service.IStmSysRoleService;
import com.shanzmoo.db.stm.entity.StmSysRoleEntity;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.stm.admin.domain.dto.StmSysRoleSaveDTO;
import com.shanzmoo.stm.admin.domain.dto.StmSysRoleUpdateDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.Valid;

/**
 * 仓储用户系统角色 控制层
 *
 * @author Gzhao 2021-03-10
 */
@Api(tags = "系统角色")
@RestController
@RequestMapping("/sys/role")
public class StmSysRoleController {

    @Autowired
    private IStmSysRoleService stmSysRoleService;

    @ApiOperation(value = "分页列表")
    @PostMapping("/pageList")
    public SHttpResponse<PageRespBase<StmSysRoleVO>> pageList(@Valid @RequestBody StmSysRoleQuery query) {
        PageRespBase<StmSysRoleVO> pageList = stmSysRoleService.queryPage(query);
        return SHttpResponse.ok(pageList);
    }

    @ApiOperation(value = "添加")
    @PostMapping("/add")
    public SHttpResponse<Boolean> add(@Valid @RequestBody StmSysRoleSaveDTO saveDto) {
        StmSysRoleEntity entity = saveDto.convertEntity();
        entity.setCreateUser(CurrentUserCache.getUserId());

        SResponse<Boolean> re = stmSysRoleService.add(entity, saveDto.getMenuIds());
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "删除")
    @GetMapping("/delete")
    public SHttpResponse<Boolean> deleteById(@RequestParam(value = "id") Integer id) {
        StmSysRoleEntity entity = new StmSysRoleEntity();
        entity.setRoleId(id);
        entity.setDeleted(true);

        stmSysRoleService.updateById(entity);
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public SHttpResponse<Boolean> update(@Valid @RequestBody StmSysRoleUpdateDTO updateDto) {
        StmSysRoleEntity entity = updateDto.convertEntity();
        entity.setCreateUser(CurrentUserCache.getUserId());

        stmSysRoleService.updateById(entity);
        return SHttpResponse.ok(Boolean.TRUE);
    }
 }
